package yf.mall.service.impl;

import com.yf.response.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import yf.mall.dao.LogRuleDao;
import yf.mall.dao.ShopLogRuleDao;
import yf.mall.dao.ShopLogTemDao;
import yf.mall.entity.LogRuleEntity;
import yf.mall.entity.ShopLogRuleEntity;
import yf.mall.entity.ShopLogTemEntity;
import yf.mall.enums.ShopLogRuleStatusEnum;
import yf.mall.service.LogRuleService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 物流费规则Service实现类
 *
 *
 *
 * @date Tue Nov 05 11:24:09 CST 2019
 */
@Service
public class LogRuleServiceImpl extends BaseServiceImpl<LogRuleEntity> implements LogRuleService {

    private LogRuleDao logRuleDao;


    @Autowired
    public void setDao(LogRuleDao logRuleDao) {
        this.logRuleDao = logRuleDao;
        super.setBaseDao(logRuleDao);
    }


    /**
     *
     *   @Override
     *     public List<UserVo> queryListVo(UserVo userVo) {
     *         return userDao.queryListVo(userVo);
     *     }
     *
     *     @Override
     *     public UserVo getVo(Long id) {
     *         return userDao.getVo(id);
     *     }

     @Override List<LogRuleVo> queryListVo(LogRuleVo logRuleVo){
     return logRuleDao.queryListVo(logRuleVo);
     }

     @Override LogRuleVo getVo(Long id){
     return logRuleDao.getVo(id);
     }
     **/

    @Autowired
    ShopLogRuleDao shopLogRuleDao;
    @Autowired
    ShopLogTemDao shopLogTemDao;
    /**
     * 内部方法
     **/
    @Override
    public BigDecimal computeLogFee(String province, BigDecimal weight,String name) {
        /*//如果是店铺的产品 获取该店物流规则
        if (logTemId != null){
            ShopLogTemEntity shopLogTemEntity = shopLogTemDao.get(logTemId);
            ShopLogRuleEntity shopLogRuleEntity = getLogRule(province, shopLogTemEntity);
            return computeShopLogFee(shopLogRuleEntity,weight);
        }else {*/

            //公司产品
            LogRuleEntity logRuleEntity = logRuleDao.findByLikeProvinceAndName(province,name);
            if(logRuleEntity==null){
                logRuleEntity = logRuleDao.findByLikeProvinceAndName(province,null);
            }
            if (logRuleEntity == null) {
                throw new RRException("传入省份有误!");
            }
            return computeLogFee(logRuleEntity,weight);

    }

    private ShopLogRuleEntity getLogRule(String province, ShopLogTemEntity shopLogTemEntity) {
        //获取物流规则
        List<ShopLogRuleEntity> ruleEntityList = shopLogRuleDao.findByTemId(shopLogTemEntity.getId());
        ShopLogRuleEntity shopLogRule = null;
        for (ShopLogRuleEntity shopLogRuleEntity : ruleEntityList) {
            //获取该省的物流规则 如果没有设置则获取默认规则
            if (shopLogRuleEntity.getStatus() == ShopLogRuleStatusEnum.YES.getIndex()){
                shopLogRule = shopLogRuleEntity;
            }
            if (shopLogRuleEntity.getProvince().contains(province)){
                shopLogRule = shopLogRuleEntity;
                break;
            }
        }
        return shopLogRule;
    }
    /*public BigDecimal computeShopLogFee(ShopLogRuleEntity shopLogRuleEntity,BigDecimal weight) {
        //在首重内
        if (weight.compareTo(new BigDecimal(shopLogRuleEntity.getFw())) < 1) {
            return shopLogRuleEntity.getFp();
        } else {
            //首重
            BigDecimal fw = new BigDecimal(shopLogRuleEntity.getFw());
            //首重价格
            BigDecimal fp = shopLogRuleEntity.getFp();
            //超出重量
            BigDecimal upWeight = weight.subtract(fw);
            //续重价格
            BigDecimal up = shopLogRuleEntity.getUp();
            //超出首重范围
            return fp.add(upWeight.multiply(up));
        }
    }*/
    public BigDecimal computeLogFee(LogRuleEntity logRuleEntity,BigDecimal weight) {
       String[] weights = logRuleEntity.getWeight().split("_");
       String[] prices = logRuleEntity.getPrice().split("_");
       BigDecimal price = new BigDecimal(0);
       for (int i = 0; i < weights.length ; i++) {
           BigDecimal s_weight = BigDecimal.valueOf(Double.parseDouble(weights[i]));
           //在区间范围内 包括区间
           if (weight.compareTo(s_weight) < 1) {
               price = BigDecimal.valueOf(Double.parseDouble(prices[i]));
               break;
           }
       }
       //超出区间范围
       if (price.compareTo(BigDecimal.ZERO) == 0) {
           //重量向上取整
           int tem = weight.setScale(0, BigDecimal.ROUND_UP).intValue();
           weight = new BigDecimal(tem);
           BigDecimal s_weg = new BigDecimal(1);
           weight = weight.subtract(s_weg);
           //去首重 * 单价
           BigDecimal j_price = weight.multiply(logRuleEntity.getUp());
           price = logRuleEntity.getFp().add(j_price);

       }
       return  price;
   }


    public static void main(String[] args) {
        LogRuleEntity logRuleEntity = new LogRuleEntity();
        logRuleEntity.setWeight("3_4");
        logRuleEntity.setPrice("8_11");
        logRuleEntity.setFp(new BigDecimal(4));
        logRuleEntity.setUp(new BigDecimal(1.8));
        System.out.println(new LogRuleServiceImpl().computeLogFee(logRuleEntity,new BigDecimal(3.9)));
    }

    /**外部方法实现**/


}
